package at.ac.tuwien.dse.health.event.listener;

import at.ac.tuwien.dse.health.event.bus.EventTarget;
import at.ac.tuwien.dse.health.event.type.Event;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;

/**
 * Logs events using a log4j {@link Logger}.
 */
public class LoggingEventListener implements EventListener {
	Logger logger;

	@Override
	public void handleEvent(EventTarget eventTarget, Event<? extends Serializable> event) {
		getLogger().info("Got event " + event);
	}

	/**
	 * Returns the {@link Logger} to use for logging events delivered to the given object.
	 *
	 * @return the logger to use
	 * @see LogManager#getLogger(Object)
	 */
	protected Logger getLogger() {
		return logger != null ? logger : (logger = LogManager.getLogger(this));
	}

	/**
	 * Sets the logger to use for logging all events subscribed.
	 *
	 * @param logger the logger to use
	 */
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
}
